import {Injectable} from '@angular/core';
import {UrlService} from '../../../common/service/url.service';
import {HttpClient} from '@angular/common/http';
import {ToastService} from 'ng-zorro-antd-mobile';

@Injectable({
  providedIn: 'root'
})
export class EquipmentfaultService {
  public hostname = this.urlService.host + '/boam';

  public allDataUrl = this.hostname + '/equipmentFaultAllData';
  public filterParamsUrl = this.hostname + '/equipmentFaultFilterParamsData';
  public filterParamsAndPageNumberUrl = this.hostname + '/equipmentFaultDataByFilterParamsAndPageNumber';
  public addDataUrl = this.hostname + '/equipmentFaultAddData';
  public saveOneDataUrl = this.hostname + '/equipmentFaultSaveOneData';
  public editDataUrl = this.hostname + '/equipmentFaultEditData';
  public deleteDataUrl = this.hostname + '/equipmentFaultDeleteData';


  constructor(
    private urlService: UrlService,
    private http: HttpClient,
    private _toast: ToastService,
  ) {
  }

  // 获取所有的设备故障单数据
  getEquipmentFaultList(): any {
    let data;
    return new Promise((resolve, reject) => {
      this.http.get(this.allDataUrl).toPromise().then(res => {
        if (res) {
          data = res;
        }
        resolve(data);
      }, error => {
        reject(data);
      });
    });
  }

  // 根据筛选条件获取设备故障单数据
  getEquipmentFaultListByFilter(params) {
    let data;
    return new Promise((resolve, reject) => {
      this.http.post(this.filterParamsUrl, params).toPromise().then(res => {
        if (res) {
          data = res;
        }
        resolve(data);
      }, error => {
        reject(data);
      });
    });
  }

  // 根据筛选条件获取设备故障单数据-分页
  getEquipmentFaultListByFilterAndPage(params) {
    let data;
    return new Promise((resolve, reject) => {
      this.http.post(this.filterParamsAndPageNumberUrl, params).toPromise().then(res => {
        if (res) {
          data = res;
        }
        resolve(data);
      }, error => {
        reject(data);
      });
    });
  }

  // 添加整个table
  addData(data): any {
    return new Promise((resolve, reject) => {
      this.http.post(this.addDataUrl, data).toPromise().then(res => {
        if (res['code'] == 200) {
          this._toast.success('保存成功');
        } else {
          this._toast.fail('保存失败');
        }
        resolve(res);
      }, res => {
        reject(false);
      });
    });
  }

  // 保存一条设备故障单数据（新增、编辑）
  saveOneData(data, operate?: string): any {
    return new Promise((resolve, reject) => {
      this.http.post(this.saveOneDataUrl, data).toPromise().then(res => {
        if (res['code'] == 200) {
          switch (operate) {
            case 'next' :
              // 语句
              this._toast.success('处理成功');
              break; // 可选
            case 'previous' :
              // 语句
              this._toast.success('撤销成功');
              break; // 可选
            // 你可以有任意数量的case语句
            default : // 可选
              // 语句
              this._toast.success('保存成功');
          }
        } else {
          this._toast.fail('保存失败');
        }
        resolve(res);
      }, res => {
        reject(false);
      });
    });
  }

  // 保存一条设备故障单数据
  editData(data): any {
    return new Promise((resolve, reject) => {
      this.http.post(this.editDataUrl, data).toPromise().then(res => {
        if (res['code'] == 200) {
          this._toast.success('保存成功');
        } else {
          this._toast.fail('保存失败');
        }
        resolve(res);
      }, res => {
        reject(false);
      });
    });
  }

  deleteData(id: string): any {
    return new Promise((resolve, reject) => {
      this.http.post(this.deleteDataUrl, {id: id}).toPromise().then(res => {
        if (res['code'] == 200) {
          this._toast.success('删除成功');
          resolve(res);
        }
        // TODO 删除时校验是否被引用
        else {
          this._toast.fail('删除失败');
          reject(false);
        }
      }, res => {
        reject(false);
      });
    });
  }
}
